package GUI;

import Classes.Cliente;
import Controle.ControleCliente;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Janela que controla métodos de inserção/edição de Atividades como também o
 * fluxo de arquivos e exibição das mesmas.
 *
 * @author Henrique
 */
public class NovoCliente extends javax.swing.JDialog {

    int codigo;
    

    /**
     * Cria um novo objeto Atividade caso o cod passado como paramêtro seja -1
     * (nova atividade), e edita algum existente caso seja outro número (código
     * real da atividade). Salva o mesmo no arquivo de atividades.
     *
     * @param cod código da atividade (-1 caso nova atividade)
     */
    public NovoCliente(int cod) {
        Date dataMomento;
        initComponents();
        codigo = cod;
        if (codigo == -1) {
        } else {
            try {
                ControleCliente cc = new ControleCliente();
                ArrayList<Cliente> cl = cc.listaClientes();
                for (int i = 0; i < cl.size(); i++) {
                    if (codigo == cl.get(i).getCodigo()) {
                        jTextNome.setText(cl.get(i).getNome());
                        campoCpf.setText(cl.get(i).getCpf());
                        jTextCelular.setText(cl.get(i).getTelefone());
                    }
                }
            } catch (SQLException ex) {
                Logger.getLogger(NovoCliente.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        setModal(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextNome = new javax.swing.JTextField();
        jTextCelular = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        cancelaAtiv = new javax.swing.JButton();
        botaoSalvar = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        campoCpf = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Cadastrar Novo Mensalista - Power Park");
        setModal(true);
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("Cadastrar Novo Cliente");

        jLabel2.setText("Nome:");

        jLabel4.setText("Telefone:");

        cancelaAtiv.setText("Cancelar");
        cancelaAtiv.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelaAtivActionPerformed(evt);
            }
        });

        botaoSalvar.setText("Salvar");
        botaoSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoSalvarActionPerformed(evt);
            }
        });

        jLabel7.setText("CPF:");

        campoCpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoCpfActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(14, 14, 14)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cancelaAtiv)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(botaoSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(23, 23, 23)
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextNome)
                                    .addComponent(campoCpf)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextCelular)))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jTextCelular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cancelaAtiv)
                    .addComponent(botaoSalvar))
                .addGap(17, 17, 17))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-416)/2, (screenSize.height-226)/2, 416, 226);
    }// </editor-fold>//GEN-END:initComponents

    private void cancelaAtivActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelaAtivActionPerformed
        dispose();
    }//GEN-LAST:event_cancelaAtivActionPerformed

    private void botaoSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoSalvarActionPerformed
        if (codigo == -1) {
            Cliente c = new Cliente();
            c.setCpf(campoCpf.getText());
            c.setNome(jTextNome.getText());
            c.setTelefone(jTextCelular.getText());
            c.setStatusCliente(1);
            //c.setDataCadastro(dataMomento.);            

            
            try {

                ControleCliente cc = new ControleCliente();                

                cc.adicionaCliente(c);
                //cc = new ControleCliente();
                
                
//                clientes = cc.listaClientes();
//                for (int i = 0; i < clientes.size(); i++) {
//                    if (clientes.get(i).getNome().equals(c.getNome()))
//                    {
//                        System.out.println("codigo  " + clientes.get(i).getCodigo());
//                    }
//                }

            } catch (SQLException ex) {
                Logger.getLogger(NovoCliente.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
        }

        CadastroCliente.atualizaTabela();

        dispose();
    }//GEN-LAST:event_botaoSalvarActionPerformed

    private void campoCpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoCpfActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoCpfActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoSalvar;
    private javax.swing.JTextField campoCpf;
    private javax.swing.JButton cancelaAtiv;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField jTextCelular;
    private javax.swing.JTextField jTextNome;
    // End of variables declaration//GEN-END:variables
    private ArrayList<Cliente> clientes;
}